<template>
  <div class="app-container">
    <el-row :gutter="24">
      <el-col :span="6" :offset="20"><el-button type="primary" size="mini" @click="handleAdd">添加标签</el-button></el-col>
    </el-row>
    <el-table
      v-loading="listLoading"
      style="margin-top: 20px"
      :data="list"
      size="mini"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope" align="center">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="标签名" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px"
      :current-page.sync="page.current"
      :page-sizes="[10,20,50,100, 200, 300, 400]"
      :page-size="page.size"
      layout="total,sizes, prev, pager, next"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { classesList, classesEdit, classesAdd, classesDel } from '@/api/classes'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      page: {
        size: 20,
        current: 1,
        total: 1
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      classesList(this.page).then(response => {
        this.list = response.body
        this.listLoading = false
        this.page.total = response.totalCount
      })
    },
    handleEdit(row) {
      this.$prompt('请输入标签名称', '编辑标签', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        inputValue: row.name
      }).then(({ value }) => {
        classesEdit({ id: row.id, name: value }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '标签修改成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: '标签修改失败'
            })
          }
        })
      })
    },
    handleDelete(row) {
      this.$confirm('是否确认删除该标签', '删除标签', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        classesDel({ id: row.id }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '标签添加成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      })
    },
    handleAdd() {
      this.$prompt('请输入标签名称', '新增标签', {
        confirmButtonText: '保存',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        inputValidator: this.tagNameValidator
      }).then(({ value }) => {
        classesAdd({ name: value }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '标签添加成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      })
    },
    tagNameValidator(val) {
      if (val !== '') {
        return true
      } else {
        return '请输入标签名称'
      }
    },
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    }
  }
}
</script>
